package com.ruoyi.system.apiController;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.Account;
import com.ruoyi.system.domain.YyAdmin;
import com.ruoyi.system.domain.YyDoctor;
import com.ruoyi.system.domain.YyPatient;
import com.ruoyi.system.enums.RoleEnum;
import com.ruoyi.system.service.IYyAdminService;
import com.ruoyi.system.service.IYyDoctorService;
import com.ruoyi.system.service.IYyPatientService;
import com.ruoyi.system.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 基础前端接口
 */
@RestController
@RequestMapping("/api")
public class WebController {
    @Autowired
    private IYyPatientService yyPatientService;
    @Autowired
    private IYyDoctorService yyDoctorService;
    @Autowired
    private IYyAdminService yyAdminService;


    /**
     * 患者登录
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            @RequestParam("role") String role) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        account.setRole(role);
        if (ObjectUtil.isEmpty(account.getUsername()) || ObjectUtil.isEmpty(account.getPassword())
                || ObjectUtil.isEmpty(account.getRole())){
            return AjaxResult.error("参数缺失");
        }
        if(RoleEnum.ADMIN.name().equals(account.getRole())){
            YyAdmin yyAdmin = yyAdminService.login(account);
            if (yyAdmin != null){
                HashMap<String, Object> map = new HashMap<>();
                map.put("id",yyAdmin.getId());
                map.put("name",yyAdmin.getName());
                //使用JWT工具类，生成身份令牌
                String token = JwtUtils.generateJwt(map);
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("token",token);
                hashMap.put("user",yyAdmin);
                return AjaxResult.success(hashMap);
            }
        }
        if (RoleEnum.DOCTOR.name().equals(account.getRole())){
            YyDoctor result = yyDoctorService.login(account);
            if (result != null){
                HashMap<String, Object> map = new HashMap<>();
                map.put("id",result.getDoctorId());
                map.put("name",result.getAccount());
                //使用JWT工具类，生成身份令牌
                String token = JwtUtils.generateJwt(map);
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("token",token);
                hashMap.put("user",result);
                return AjaxResult.success(hashMap);
            }
        }
        if (RoleEnum.PATIENT.name().equals(account.getRole())){
            YyPatient result = yyPatientService.login(account.getUsername(), account.getPassword());
            if (result != null){
                HashMap<String, Object> map = new HashMap<>();
                map.put("id",result.getPatientId());
                map.put("name",result.getName());
                //使用JWT工具类，生成身份令牌
                String token = JwtUtils.generateJwt(map);
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("token",token);
                hashMap.put("user",result);
                return AjaxResult.success(hashMap);
            }
        }

        return AjaxResult.error("用户名或密码错误");
    }
    /**
     * 注册
     */
   /* @PostMapping("/register")
    public Result register(@RequestBody Account account) {
        if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword())
                || ObjectUtil.isEmpty(account.getRole())) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        if (RoleEnum.ADMIN.name().equals(account.getRole())) {
            adminService.register(account);
        }
        if (RoleEnum.STUDENT.name().equals(account.getRole())) {
            studentService.register(account);
        }
        return Result.success();
    }

    *//**
     * 修改密码
     *//*
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account) {
        if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword())
                || ObjectUtil.isEmpty(account.getNewPassword())) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        if (RoleEnum.ADMIN.name().equals(account.getRole())) {
            adminService.updatePassword(account);
        }
        if (RoleEnum.TEACHER.name().equals(account.getRole())) {
            teacherService.updatePassword(account);
        }
        if (RoleEnum.STUDENT.name().equals(account.getRole())) {
            studentService.updatePassword(account);
        }
        return Result.success();
    }*/

}
